package com.eascs.spider.yuqing.common.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

	public static boolean isNullOrEmpty(String msg){
		return (msg==null || msg == "")?true:false;
	}

	public String string2Unicode(String string) {
	    StringBuffer unicode = new StringBuffer();
	    for (int i = 0; i < string.length(); i++) {
	        // 取出每一个字符
	        char c = string.charAt(i);
	        // 转换为unicode
	        unicode.append("\\u" + Integer.toHexString(c));
	    }
	    return unicode.toString();
	}

	public String unicode2String(String unicode) {
	    StringBuffer string = new StringBuffer();
	    String[] hex = unicode.split("\\\\u");
	    for (int i = 1; i < hex.length; i++) {
	        // 转换出每一个代码点
	        int data = Integer.parseInt(hex[i], 16);
	        // 追加成string
	        string.append((char) data);
	    }
	    return string.toString();
	}
	
	public static void main(String[] args) throws Exception{
		File file = new File("src/main/resources/test.txt");
		StringBuilder result = new StringBuilder();
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            while((s = br.readLine())!=null){//使用readLine方法，一次读一行
                result.append(System.lineSeparator()+s);
            }
            br.close();    
        }catch(Exception e){
            e.printStackTrace();
        }
        String html = result.toString().replaceAll("\\\\", "");
        
        Pattern pattern = Pattern.compile("href=\"//weibo.com/.*?\"");
        Matcher matcher = pattern.matcher(html);
        while(matcher.find()){
        	String url = matcher.group();
        	if(url.split("/").length >=4){
        		try {
        			//校验第四栏是否是数字,发生NumberFormatException则跳过
					Integer.valueOf(url.split("/")[3]);
					System.out.println(url);
				} catch (NumberFormatException e) {
					// TODO: handle exception
				}
        	}
        }
	}
}
